Subroutine output_files(step)
use type
use var
use parallel
implicit none
include 'mpif.h'
integer :: i, j, k, indx, outlQ, Nsend, Nrec(size), displas(size+1)
integer :: N, NN
integer, intent(in) :: step
real(dp), dimension(5) ::   q_tmp
real(dp),dimension(3) :: temp_scalar,temp_director
real(dp),dimension(:,:), allocatable :: Tscalar, Tdirector
real(dp),dimension(:,:), allocatable :: scalar, director
real(sp),dimension(:,:), allocatable :: qsingle

If (output>=1.and.ID==master) then
   outlQ = 400 + ID
   open ( outlQ , FILE = "lQ" ,form='UNFORMATTED', access='SEQUENTIAL')
   allocate (qsingle(6,Mnodes))

   do i=1,Mnodes
      qsingle(:,i) = real(q(:,i))
   enddo

   write(outlQ)"traj frame"
   write(outlQ)frame,step
   write(outlQ)qsingle

   deallocate(qsingle)
endif

if (output>=2) then

if (ID==master) allocate(Tscalar(3,length*size), Tdirector(3,length*size))
allocate(scalar(3,length), director(3,length))
scalar = 0.d0
director = 0.d0

do indx=1,length
   q_tmp = q(1:5,indx)
   call eigen ( q_tmp(:), temp_scalar, temp_director(:))
   scalar(:,indx) = temp_scalar
   if (temp_director(3)<0.0) temp_director = -1 * temp_director
   director(:,indx) = temp_director
enddo

CALL MPI_BARRIER(MPI_COMM_NEW, ierr)
CALL MPI_GATHER (scalar, 3*length, MPI_DOUBLE_PRECISION, Tscalar, 3*length, MPI_DOUBLE_PRECISION, master, MPI_COMM_NEW, IERR)
CALL MPI_GATHER (director, 3*length, MPI_DOUBLE_PRECISION, Tdirector, 3*length, MPI_DOUBLE_PRECISION, master, MPI_COMM_NEW, IERR)

if (ID==master) call paraview()
CALL MPI_BARRIER(MPI_COMM_NEW, ierr)
if (ID==master) deallocate(Tscalar, Tdirector)
deallocate(scalar, director)
endif

contains
subroutine paraview()
use type
use var
implicit none
integer (i4b) :: i,iout=50,j,k,label
real(dp) :: r2,lbox,cx,x,y,z,eta, temp_scalar(3), temp_director(3)
!********************************************************************
Character(len=10) :: namefile1, namefile2
Character(len=20), Parameter :: sfile_3= 'surface',end='vtk'  


open(iout,FILE=trim(adjustr(sfile_3))//"."//trim(adjustl(end)),STATUS='replace')

!*************************************************************************************

write(iout,100)'# vtk DataFile Version 3.0'
write(iout,100)'vtk output'
write(iout,100)'ASCII'
write(iout,100)'DATASET STRUCTURED_GRID'
write(iout,110)'DIMENSIONS',dime(1),dime(2),dime(3)
write(iout,120)'POINTS',dime(1)*dime(2)*dime(3) ,'  float'

do k=1,dime(3)
   do j=1,dime(2)
      do i=1,dime(1)
          write(iout,151)  real(i),real(j),real(k),'  '
     enddo
   enddo
enddo


write(iout,*)
write(iout,120)'POINT_DATA', dime(1)*dime(2)*dime(3)
write(iout,100)'SCALARS scalars float'
write(iout,100)'LOOKUP_TABLE default'


do k=1,dime(3)
   do j=1,dime(2)
      do i=1,dime(1)
            label = 1 + (i-1) + (j-1)*dime(1) + (k-1)*dime(1)*dime(2)
            if (lb2indx(label)==0) then
               temp_scalar = Sbulk/1.5d0
            else
               temp_scalar = Tscalar(:,lb2indx(label))
            endif
            write(iout,152 )  maxval(temp_scalar)*1.5d0,'  '
      enddo
   enddo
enddo

write(iout,*)
write(iout,100)'SCALARS biaxial float'
write(iout,100)'LOOKUP_TABLE default'


do k=1,dime(3)
   do j=1,dime(2)
      do i=1,dime(1)
            label = 1 + (i-1) + (j-1)*dime(1) + (k-1)*dime(1)*dime(2)
            if (lb2indx(label)==0) then
               temp_scalar = 0.0
               temp_director = 0.0
            else
               temp_scalar = Tscalar(:,lb2indx(label))
               temp_director = Tdirector(:,lb2indx(label))
            endif
            write(iout,152 ) temp_scalar(2)*3.d0 + maxval(temp_scalar)*1.5d0,'  ' 
      enddo
   enddo
enddo


write(iout,*)
write(iout,100)'VECTORS directorsz float'

do k=1,dime(3)
   do j=1,dime(2)
      do i=1,dime(1)
            label = 1 + (i-1) + (j-1)*dime(1) + (k-1)*dime(1)*dime(2)
            if (lb2indx(label)==0) then
               temp_director = 0.0
            else
               temp_director = Tdirector(:,lb2indx(label))
            endif
            write(iout,153 )  temp_director(1),temp_director(2),temp_director(3),'  '
      enddo
   enddo
enddo

close (iout)
!*****************************************************************************************
!*****************************************************************************************
100 format('',A)
120 format('',A,I12,A)
110 format('',A, I6, 1X, I6, 1X, I6)
151 format (3f12.6 ,A)
152 format (f12.6 ,A)
153 format (3f12.6 ,A)
end subroutine paraview
end subroutine output_files
